<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px; margin:0px; text-align:center}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

$matchId = $_GET["matchId"];
@ $type = $_GET["type"];
@ $date = $_GET["date"];

if ($matchId == "") {
	return;
}
if ($date == "") {
	$date = date("ym");
} else {
	$date = explode("-", $date);
	$year = substr($date[0], -2);
	$date = $year.$date[1];
}

//连接数据库
$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

$t1_h_s_s = NULL;
$t1_h_s_n = NULL;
$t1_h_lb_s = NULL;
$t1_h_lb_n = NULL;
$t1_h_ass_s = NULL;
$t1_h_ass_n = NULL;
$t1_h_ste_s = NULL;
$t1_h_ste_n = NULL;
$t1_h_blo_s = NULL;
$t1_h_blo_n = NULL;
$t1_lb = NULL;
$t1_field = NULL;
$t1_three = NULL;
$t1_free = NULL;
$t1_jshots = NULL;
$t1_dunks = NULL;
$t1_layups = NULL;
$t1_tipins = NULL;
$t1_hooks = NULL;
$t1_bbeaters = NULL;
$t1_tecf = NULL;
$t1_flag = NULL;
$t1_eject = NULL;
$t1_disq = NULL;
$t2_h_s_s = NULL;
$t2_h_s_n = NULL;
$t2_h_lb_s = NULL;
$t2_h_lb_n = NULL;
$t2_h_ass_s = NULL;
$t2_h_ass_n = NULL;
$t2_h_ste_s = NULL;
$t2_h_ste_n = NULL;
$t2_h_blo_s = NULL;
$t2_h_blo_n = NULL;
$t2_lb = NULL;
$t2_field = NULL;
$t2_three = NULL;
$t2_free = NULL;
$t2_jshots = NULL;
$t2_dunks = NULL;
$t2_layups = NULL;
$t2_tipins = NULL;
$t2_hooks = NULL;
$t2_bbeaters = NULL;
$t2_tecf = NULL;
$t2_flag = NULL;
$t2_eject = NULL;
$t2_disq = NULL;

if ($type == "NBA") {
	$url = "http://sports.sinajs.cn/" . $date . "/" . $matchId . ".js?" . rand();

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$matchStats = curl_exec($ch);

	curl_close($ch);
	if($matchStats == ""){
		//切换到高速模式
		$url = "http://sports.sina.com.cn/nba/iframe/js/" . $date . "/" . $matchId . ".js?" . rand();
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		$matchStats = curl_exec($ch);
		curl_close($ch);
	}
	$matchStats = iconv("GBK", "UTF-8", $matchStats);
	$matchStats = str_replace("basket_live_render_stats(", "", $matchStats);
	$matchStats = str_replace(");", "", $matchStats);
	$matchStats = "{" . substr($matchStats, stripos($matchStats, "},") + 2, strlen($matchStats));

	$matchStats = str_replace(",,", "", $matchStats);
	$matchStats = str_replace("'", "", $matchStats);
	$matchStats = str_replace(" ", "", $matchStats);
	$matchStats = str_replace("\t", "", $matchStats);
	$matchStats = str_replace("\r\n", "", $matchStats);

	$matchStats = str_replace(",", "\",\"", $matchStats);
	$matchStats = str_replace(":", "\":\"", $matchStats);

	$matchStats = str_replace("\"{", "{", $matchStats);
	$matchStats = str_replace("{", "{\"", $matchStats);

	$matchStats = str_replace("}", "\"}", $matchStats);
	$matchStats = str_replace("}\"", "}", $matchStats);

	$matchStats = str_replace("\"[", "[", $matchStats);
	$matchStats = str_replace("[", "[\"", $matchStats);

	$matchStats = str_replace("]\"", "]", $matchStats);
	$matchStats = str_replace("]", "\"]", $matchStats);

	$matchStats = json_decode($matchStats);

	$t1_h_s_s = $matchStats->t1->high->s->s;
	$t1_h_s_n = $matchStats->t1->high->s->n;
	$t1_h_lb_s = $matchStats->t1->high->lb->s;
	$t1_h_lb_n = $matchStats->t1->high->lb->n;
	$t1_h_ass_s = $matchStats->t1->high->ass->s;
	$t1_h_ass_n = $matchStats->t1->high->ass->n;
	$t1_h_ste_s = $matchStats->t1->high->ste->s;
	$t1_h_ste_n = $matchStats->t1->high->ste->n;
	$t1_h_blo_s = $matchStats->t1->high->blo->s;
	$t1_h_blo_n = $matchStats->t1->high->blo->n;
	$t1_lb = $matchStats->t1->lb;
	$t1_field = $matchStats->t1->field;
	$t1_three = $matchStats->t1->three;
	$t1_free = $matchStats->t1->free;
	$t1_jshots = $matchStats->t1->jshots;
	$t1_dunks = $matchStats->t1->dunks;
	$t1_layups = $matchStats->t1->layups;
	$t1_tipins = $matchStats->t1->tipins;
	$t1_hooks = $matchStats->t1->hooks;
	$t1_bbeaters = $matchStats->t1->bbeaters;
	$t1_tecf = $matchStats->t1->tecf;
	$t1_flag = $matchStats->t1->flag;
	$t1_eject = $matchStats->t1->eject;
	$t1_disq = $matchStats->t1->disq;

	$t2_h_s_s = $matchStats->t2->high->s->s;
	$t2_h_s_n = $matchStats->t2->high->s->n;
	$t2_h_lb_s = $matchStats->t2->high->lb->s;
	$t2_h_lb_n = $matchStats->t2->high->lb->n;
	$t2_h_ass_s = $matchStats->t2->high->ass->s;
	$t2_h_ass_n = $matchStats->t2->high->ass->n;
	$t2_h_ste_s = $matchStats->t2->high->ste->s;
	$t2_h_ste_n = $matchStats->t2->high->ste->n;
	$t2_h_blo_s = $matchStats->t2->high->blo->s;
	$t2_h_blo_n = $matchStats->t2->high->blo->n;
	$t2_lb = $matchStats->t2->lb;
	$t2_field = $matchStats->t2->field;
	$t2_three = $matchStats->t2->three;
	$t2_free = $matchStats->t2->free;
	$t2_jshots = $matchStats->t2->jshots;
	$t2_dunks = $matchStats->t2->dunks;
	$t2_layups = $matchStats->t2->layups;
	$t2_tipins = $matchStats->t2->tipins;
	$t2_hooks = $matchStats->t2->hooks;
	$t2_bbeaters = $matchStats->t2->bbeaters;
	$t2_tecf = $matchStats->t2->tecf;
	$t2_flag = $matchStats->t2->flag;
	$t2_eject = $matchStats->t2->eject;
	$t2_disq = $matchStats->t2->disq;

} else {
	$url = "http://sports.sinajs.cn/livecast/js/basketball_" . $matchId . ".js?" . rand();

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$matchStats = curl_exec($ch);

	curl_close($ch);
	$matchStats = iconv("GBK", "UTF-8", $matchStats);

	$matchStats = str_replace("\n", "", $matchStats);
	$matchStats = str_replace("var c1 = ['", "", $matchStats);
	$matchStats = str_replace("var c2 = ['", "", $matchStats);
	$matchStats = str_replace("var s1 = ['", "", $matchStats);
	$matchStats = str_replace("']", "", $matchStats);

	$matchStats = explode(";", $matchStats);
	$c1 = $matchStats[0];
	$c2 = $matchStats[1];

	$c1 = explode("','", $c1);
	$c2 = explode("','", $c2);

	$t1_h_s = explode(" ", $c1[0]);
	$t1_h_lb = explode(" ", $c1[1]);
	$t1_h_ass = explode(" ", $c1[2]);
	$t1_h_ste = explode(" ", $c1[3]);
	$t1_h_blo = explode(" ", $c1[5]);
	$t2_h_s = explode(" ", $c2[0]);
	$t2_h_lb = explode(" ", $c2[1]);
	$t2_h_ass = explode(" ", $c2[2]);
	$t2_h_ste = explode(" ", $c2[3]);
	$t2_h_blo = explode(" ", $c2[5]);

	$t1_h_s_s = $t1_h_s[1];
	$t1_h_s_n = $t1_h_s[0];
	$t1_h_lb_s = $t1_h_lb[1];
	$t1_h_lb_n = $t1_h_lb[0];
	$t1_h_ass_s = $t1_h_ass[1];
	$t1_h_ass_n = $t1_h_ass[0];
	$t1_h_ste_s = $t1_h_ste[1];
	$t1_h_ste_n = $t1_h_ste[0];
	$t1_h_blo_s = $t1_h_blo[1];
	$t1_h_blo_n = $t1_h_blo[0];
	$t2_h_s_s = $t2_h_s[1];
	$t2_h_s_n = $t2_h_s[0];
	$t2_h_lb_s = $t2_h_lb[1];
	$t2_h_lb_n = $t2_h_lb[0];
	$t2_h_ass_s = $t2_h_ass[1];
	$t2_h_ass_n = $t2_h_ass[0];
	$t2_h_ste_s = $t2_h_ste[1];
	$t2_h_ste_n = $t2_h_ste[0];
	$t2_h_blo_s = $t2_h_blo[1];
	$t2_h_blo_n = $t2_h_blo[0];

}

//先清空当前比赛场次的数据
$sql = "INSERT INTO live_team_stats_b (matchId, t1_h_s_s, t1_h_s_n, t1_h_lb_s, t1_h_lb_n, t1_h_ass_s, t1_h_ass_n, t1_h_ste_s, t1_h_ste_n, t1_h_blo_s, t1_h_blo_n, t1_lb, t1_field, t1_three, t1_free, t1_jshots, t1_dunks, t1_layups, t1_tipins, t1_hooks, t1_bbeaters, t1_tecf, t1_flag, t1_eject, t1_disq, t2_h_s_s, t2_h_s_n, t2_h_lb_s, t2_h_lb_n, t2_h_ass_s, t2_h_ass_n, t2_h_ste_s, t2_h_ste_n, t2_h_blo_s, t2_h_blo_n, t2_lb, t2_field, t2_three, t2_free, t2_jshots, t2_dunks, t2_layups, t2_tipins, t2_hooks, t2_bbeaters, t2_tecf, t2_flag, t2_eject, t2_disq) VALUES";
$sql .= "('" . $matchId . "', '" . $t1_h_s_s . "', '" . $t1_h_s_n . "', '" . $t1_h_lb_s . "', '" . $t1_h_lb_n . "', '" . $t1_h_ass_s . "', '" . $t1_h_ass_n . "', '" . $t1_h_ste_s . "', '" . $t1_h_ste_n . "', '" . $t1_h_blo_s . "', '" . $t1_h_blo_n . "', '" . $t1_lb . "', '" . $t1_field . "', '" . $t1_three . "', '" . $t1_free . "', '" . $t1_jshots . "', '" . $t1_dunks . "', '" . $t1_layups . "', '" . $t1_tipins . "', '" . $t1_hooks . "', '" . $t1_bbeaters . "', '" . $t1_tecf . "', '" . $t1_flag . "', '" . $t1_eject . "', '" . $t1_disq . "', '" . $t2_h_s_s . "', '" . $t2_h_s_n . "', '" . $t2_h_lb_s . "', '" . $t2_h_lb_n . "', '" . $t2_h_ass_s . "', '" . $t2_h_ass_n . "', '" . $t2_h_ste_s . "', '" . $t2_h_ste_n . "', '" . $t2_h_blo_s . "', '" . $t2_h_blo_n . "', '" . $t2_lb . "', '" . $t2_field . "', '" . $t2_three . "', '" . $t2_free . "', '" . $t2_jshots . "', '" . $t2_dunks . "', '" . $t2_layups . "', '" . $t2_tipins . "', '" . $t2_hooks . "', '" . $t2_bbeaters . "', '" . $t2_tecf . "', '" . $t2_flag . "', '" . $t2_eject . "', '" . $t2_disq . "')";
$sql .= " ON DUPLICATE KEY UPDATE t1_h_s_s = '" . $t1_h_s_s . "', t1_h_s_n = '" . $t1_h_s_n . "', t1_h_lb_s = '" . $t1_h_lb_s . "', t1_h_lb_n = '" . $t1_h_lb_n . "', t1_h_ass_s = '" . $t1_h_ass_s . "', t1_h_ass_n = '" . $t1_h_ass_n . "', t1_h_ste_s = '" . $t1_h_ste_s . "', t1_h_ste_n = '" . $t1_h_ste_n . "', t1_h_blo_s = '" . $t1_h_blo_s . "', t1_h_blo_n = '" . $t1_h_blo_n . "', t1_lb = '" . $t1_lb . "', t1_field = '" . $t1_field . "', t1_three = '" . $t1_three . "', t1_free = '" . $t1_free . "', t1_jshots = '" . $t1_jshots . "', t1_dunks = '" . $t1_dunks . "', t1_layups = '" . $t1_layups . "', t1_tipins = '" . $t1_tipins . "', t1_hooks = '" . $t1_hooks . "', t1_bbeaters = '" . $t1_bbeaters . "', t1_tecf = '" . $t1_tecf . "', t1_flag = '" . $t1_flag . "', t1_eject = '" . $t1_eject . "', t1_disq = '" . $t1_disq . "', t2_h_s_s = '" . $t2_h_s_s . "', t2_h_s_n = '" . $t2_h_s_n . "', t2_h_lb_s = '" . $t2_h_lb_s . "', t2_h_lb_n = '" . $t2_h_lb_n . "', t2_h_ass_s = '" . $t2_h_ass_s . "', t2_h_ass_n = '" . $t2_h_ass_n . "', t2_h_ste_s = '" . $t2_h_ste_s . "', t2_h_ste_n = '" . $t2_h_ste_n . "', t2_h_blo_s = '" . $t2_h_blo_s . "', t2_h_blo_n = '" . $t2_h_blo_n . "', t2_lb = '" . $t2_lb . "', t2_field = '" . $t2_field . "', t2_three = '" . $t2_three . "', t2_free = '" . $t2_free . "', t2_jshots = '" . $t2_jshots . "', t2_dunks = '" . $t2_dunks . "', t2_layups = '" . $t2_layups . "', t2_tipins = '" . $t2_tipins . "', t2_hooks = '" . $t2_hooks . "', t2_bbeaters = '" . $t2_bbeaters . "', t2_tecf = '" . $t2_tecf . "', t2_flag = '" . $t2_flag . "', t2_eject = '" . $t2_eject . "', t2_disq = '" . $t2_disq . "'";
//插入比赛比分
mysql_query($sql);

echo "球队技术统计数据采集";
?>